﻿using QDChat.Domain;
using QDChat.Interface;
using System;
using System.Collections.Generic;
using System.Text;

namespace QDChat.Service
{
    public sealed class SessionService
    {
        private static IChatSession Instance
        {
            get { return DataFactory.SessionInstance; }
        }

        /// <summary>
        /// 更新单聊的会话内容
        /// </summary>
        public static int Append(Session model)
        {
            var old = Instance.GetLast(model.FromUid, model.ToUid);
            //添加
            if (old == null)
                return Instance.Insert(model);

            old.LastMsg = model.LastMsg;
            old.SendTime = model.SendTime;

            //更新
            if (old.FromUid == model.FromUid)
            {
                old.ToMsgCount++;
                return Instance.AppendMsg(old);
            }
            if (old.ToUid == model.FromUid)
            {
                old.FromMsgCount++;
                return Instance.AppendMsg(old);
            }
            return 0;
        }

        /// <summary>
        /// 清空未读数量
        /// </summary>
        /// <param name="fromUid"></param>
        /// <param name="toUid"></param>
        public static void ResetCount(int fromUid, int toUid)
        {
            var session = Instance.GetLast(fromUid, toUid);
            if (session == null) return;

            if (session.FromUid == fromUid)
                Instance.ResetMsgCount(session.Id, 1);
            else
                Instance.ResetMsgCount(session.Id, 2);
        }

        /// <summary>
        /// 会话列表
        /// </summary>
        /// <param name="fromUser"></param>
        public static List<SessionView> GetList(int fromUid, int page, int pageSize)
        {
            return Instance.GetList(fromUid, page, pageSize);
        }
     
    }
}